package com.clock.controller.stage;

import cn.hutool.core.bean.BeanUtil;
import com.clock.common.domain.R;
import com.clock.domain.dto.UpdateUserPasswordDTO;
import com.clock.domain.entity.User;
import com.clock.domain.vo.UserBackVo;
import com.clock.service.UserService;
import com.clock.shiro.common.ShiroConstants;
import com.clock.shiro.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@Api(tags = {"用户账户接口"})
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    UserService userService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "登录接口", notes = "用户名或用户id或手机号登录")
    @CrossOrigin
    @PostMapping("/login")
    public R login(@Validated @RequestBody User user, HttpServletResponse response){
        User user1 = userService.login(user);
        if(user1 == null){
            return R.fail("账号或密码错误");
        }

        //返回token给前端
        String token = JwtUtil.createToken(user1.getUserId().toString());
        response.addHeader("Authorization", token);
        response.setHeader("Access-Control-Expose-Headers","Authorization");
        UserBackVo userBackVo = new UserBackVo();
        BeanUtil.copyProperties(user1,userBackVo);

        //将token存到redis中
        if(stringRedisTemplate.opsForValue().get(ShiroConstants.PREFIX_USER_TOKEN+user1.getUserId())!=null){
            stringRedisTemplate.delete(ShiroConstants.PREFIX_USER_TOKEN+user1.getUserId());
        }
        stringRedisTemplate.opsForValue().set(ShiroConstants.PREFIX_USER_TOKEN+user1.getUserId() , token, 2, TimeUnit.HOURS);
        return R.ok(userBackVo);
    }

    @ApiOperation(value = "登出接口", notes = "不用参数")
    // 退出
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        SecurityUtils.getSubject().logout();
        return R.ok();
    }

    @ApiOperation(value = "注册接口", notes = "前端需要做一次参数校验")
    //注册
    @PostMapping("/register")
    public R register(@RequestBody @Validated User user){
        //注册用户
        User user1 = userService.insert(user);
        if(user1==null){
            return R.fail("注册账户无效");
        }
        return R.ok();
    }

    @ApiOperation(value = "判断注册信息接口", notes = "对关键信息判重,对邮箱和电话号码正则判断，建议前端也做一次")
    //判断用户的注册信息是否合法
    @PostMapping("/checkUserInf")
    public R checkUserInf(@RequestBody User user){
        String infCheck = userService.userInfCheck(user);
        if(infCheck!=null){
            return R.fail(infCheck);
        }
        return R.ok();
    }

    @ApiOperation(value = "修改密码", notes = "需要用户id和用户新密码和用户旧密码")
    //判断用户的注册信息是否合法
    @PostMapping("/updatePassword")
    public R updatePassword(@Validated @RequestBody UpdateUserPasswordDTO updateUserPasswordDTO){
        Boolean updatePassword = userService.updatePassword(updateUserPasswordDTO);
        if(updatePassword){
            return R.ok("修改成功");
        }else{
            return R.fail("修改失败");
        }
    }

}
